package com.wnxy.pawnbackgroundsystem.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wnxy.pawnbackgroundsystem.entity.Goods;
import com.wnxy.pawnbackgroundsystem.param.goodsParam.GetGoods;
import com.wnxy.pawnbackgroundsystem.param.goodsParam.GoodsParam;
import com.wnxy.pawnbackgroundsystem.response.Result;
import com.wnxy.pawnbackgroundsystem.service.IGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品详情表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-10-14
 */
@RestController
@RequestMapping("/goods")
@Slf4j
@CrossOrigin("*")
public class GoodsController {

    @Resource
    private IGoodsService goodsService;

    @PostMapping("queryAllGoods")
    public Result<Goods> queryAllGoods(@RequestBody GetGoods getGoods){
        log.info("查询所有商品控制层：查询所有商品信息 参数={}",getGoods);
        IPage byPage = goodsService.findAllGoodsByPage( getGoods.getGoodsParam(),getGoods.getPageDto());
        log.info("查询所有商品控制层,返回前端的参数bypage:{}",byPage.getRecords());
        if(byPage!=null) {
            return Result.success(Goods.class,byPage);
        }
        return Result.fail("500","查询为空");
    }

    @PostMapping("addGoods")
    public Result addGoods(@RequestBody GetGoods getGoods){
        log.info("添加商品控制层：接受到的参数{}",getGoods.getGoodsParam());
        Goods goods = goodsService.addGoods(getGoods.getGoodsParam());
        return Result.success(Goods.class,"添加成功",goods);
    }


    @PostMapping("updateGoods")
    public Result updategoods(@RequestBody GoodsParam goodsParam){
        log.info("修改商品状态控制层");

        Integer i = goodsService.updateGoods(goodsParam);
        if(i>0){
            return Result.success("修改成功");
        }
        return Result.fail("502","修改商品状态失败");
    }

    @GetMapping("delateGoods")
    public Result delGoods(Integer ids){
        log.info("删除商品控制层接受到的参数，ids:{}",ids);

        Integer i = goodsService.deleteGood(ids);
        if(i>0){
            return Result.success("删除成功");
        }
        return Result.fail("503","删除失败");
    }

}

